//
//  CXNavigationController.swift
//  XinaWeibo
//
//  Created by chenxuan on 2017/4/4.
//  Copyright © 2017年 chenxuan. All rights reserved.
//

import UIKit

class CXNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationBar.isHidden = true

        // Do any additional setup after loading the view.
    }
    
    /// 重写 push 方法，所有的 push 动作都会调用此方法！
    /// viewController 是被 push 的控制器，设置他的左侧的按钮作为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        // 如果不是栈底控制器才需要隐藏，根控制器不需要处理
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            if let vc = viewController as? CXBaseViewController {
                var title = "返回"
                
                if childViewControllers.count == 1 {
                     title = childViewControllers.first?.title ?? "返回"
                }
                
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBack: true)
                
            }
        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
   @objc private func popToParent() {
        popViewController(animated: true)
    }

}
